﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.TcCommands.TcReplaceJt.CCoTcReplaceJtCmdEnabler
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System.Collections.ObjectModel;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.PrivateImplementationDetails;
using Tecnomatix.Planning;
using UiInternalControls;


namespace DnProcessSimulateCommands.TcCommands.TcReplaceJt;

public class CCoTcReplaceJtCmdEnabler : ITxCommandEnabler
{
  public bool Enable => this.ShouldEnable();

  private bool ShouldEnable()
  {
    bool flag = false;
    if (this.IsSupportedByPlatform)
    {
      TxObjectList allItems = TxApplication.ActiveSelection.GetAllItems();
      flag = ((Collection<ITxObject>) allItems).Count == 1;
      for (int index = 0; index < ((Collection<ITxObject>) allItems).Count & flag; ++index)
      {
        if (!(((Collection<ITxObject>) allItems)[index] is ITxComponent component))
          flag = false;
        else if (!TxEngineeringDataInternal.ObjectIsResourceInstance((ITxObject) component))
          flag = false;
        else if (TcCommandsUtility.IsEquipment((ITxObject) component) && !TcCommandsUtility.IsLeafComponent((ITxObject) component))
          flag = false;
        else if (TcCommandsUtility.IsPrototypeAssignment((ITxObject) component))
          flag = false;
        else if (component is TxCable || component is TxHuman)
          flag = false;
        else if (TcCommandsUtility.IsInModeling(component))
          flag = this.IsEnabledWhileInModeling(component) && TxTcUtilities.CanUpdateItemRevisionOnEndModeling((ITxObject) component);
        else if (!TxTcUtilities.CanUpdateItemRevisionOnEndModeling((ITxObject) component))
          flag = false;
        else if (this.InstancePrototypeHasNonInstanceOccurrences(component))
          flag = false;
      }
    }
    return flag;
  }

  private bool IsSupportedByPlatform
  {
    get => TxTcUtilities.IsTcAnyPlatform && !TxTcStudyReservation.IsReadOnly();
  }

  private bool InstancePrototypeHasNonInstanceOccurrences(ITxComponent component)
  {
    bool flag = false;
    if (component is ITxProcessModelObject processModelObject && processModelObject.PlanningRepresentation is ITxPlanningInstance planningRepresentation)
      flag = TxTcUtilities.PrototypeHasNonInstanceOccurrences(planningRepresentation.Prototype);
    return flag;
  }

  private bool IsEnabledWhileInModeling(ITxComponent component)
  {
    bool flag = true;
    if (((ITxObject) component).GetAttribute("ORIGINAL_PATH_FOR_LOCAL_COMPONENT") != null && (!(((ITxObject) component).GetAttribute("ORIGINAL_IS_EMPTY_FOR_LOCAL_COMPONENT") is TxStringAttribute attribute) || attribute.Value != "true"))
      flag = false;
    return flag;
  }
}
